package com.gw.vip.server.service;

import com.gw.vip.common.exception.ExceptionEnums;
import com.gw.vip.common.exception.ServiceException;
import com.gw.vip.common.util.DateTools;
import com.gw.vip.server.bo.IssuInfo;
import com.gw.vip.server.bo.MailSenderInfo;
import com.gw.vip.server.po.MsgTemplateInfo;
import com.gw.vip.server.service.dao_service.MsgTemplateInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author GW
 * @description
 * @since 2022-07-07
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class MailService {

    private final MsgTemplateInfoService msgTemplateInfoService;
    private final JavaMailSender javaMailSender;
    private final MailSenderInfo mailSenderInfo;
    private final IssuInfo issuInfo;

    @Async
    public void sendTxnInfo(String to, String nickName, LocalDateTime txnTime, String txnType, BigDecimal txnAmt, BigDecimal balAmt){
        MsgTemplateInfo userTemplateInfo = msgTemplateInfoService.getById("txnInfo");
        if (userTemplateInfo==null){
            log.error("消息模板不存在！");
            throw new ServiceException(ExceptionEnums.ERROR_PARAMS, "消息模板不存在！");
        }

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(mailSenderInfo.getUserName());
        message.setTo(to);
        message.setSubject(userTemplateInfo.getSubject());

        String text = userTemplateInfo.getMessage();
        Map<String, String> params = new HashMap<>(5);
        params.put("nickName", nickName);
        params.put("issuName", issuInfo.getName());
        params.put("issuId", issuInfo.getId());
        params.put("txnTime", DateTools.time2str19(txnTime));
        params.put("txnType", txnType);
        params.put("txnAmt", txnAmt.toString());
        params.put("balAmt", balAmt.toString());

        text = this.parse(text, params);
        message.setText(text);
        log.info("邮件内容:{}", text);
        try {
            javaMailSender.send(message);
        } catch (MailException e) {
            e.printStackTrace();
            throw new ServiceException("邮件发送失败！");
        }
    }

    //--------------------------------------------- 辅助函数 ----------------------------------------------//

    private String parse(String content, Map<String, String> kvs) {
        String pattern = "\\$\\{(.*?)}";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(content);
        StringBuffer sr = new StringBuffer();
        while (m.find()) {
            String group = m.group().substring(2, m.group().length()-1);
            m.appendReplacement(sr, kvs.get(group));
        }
        m.appendTail(sr);
        return sr.toString();
    }
}
